#ifndef __LIST_H
#define __LIST_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>


struct list_node
{
    struct list_node *next;
    struct list_node *prev;
};

typedef struct list_node list;
typedef struct list_node list_node;

static inline void list_init(list *l)
{
    l->next = l->prev = l;
}

static inline void list_insert_after(list *l, list *n)
{
    l->next->prev = n;
    n->next = l->next;

    l->next = n;
    n->prev = l;
}
static inline void list_insert_before(list *l, list *n)
{
    l->prev->next = n;
    n->prev = l->prev;
    n->next = l;
    l->prev = n;
}
static inline void list_remove(list *n)
{
    n->prev->next = n->next;
    n->next->prev = n->prev;
    n->next = n->prev = n;
}
static inline int list_len(list *n)
{
    int len = 0;
    list *p = n;
    while (p->next != n)
    {
        p = p->next;
        len++;
    }
    return len;
}
static inline int list_isempty(list *n)
{
    return (n->next == n);
}
#define container_of(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
#define GET_DEVICE(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
#define list_for_each_entry(pos, head, member) \
    for (pos = container_of((head)->next, typeof(*pos), member); \
         &pos->member != (head); \
         pos = container_of(pos->member.next, typeof(*pos), member))
#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

#endif
